package com.common.base.tools.obj

import android.os.CountDownTimer
/**
 * Created by zsf on 2023/3/28 10:06
 * *******************
 *    定时器
 * *******************
 */
class CountDownTimerManager {
    lateinit var countDownJob: CountDownTimer

    private constructor()

    constructor(callback: (Long) -> Unit, onFinish: () -> Unit, time: Long = 30) {
        countDownJob = object : CountDownTimer(time * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                callback(millisUntilFinished / 1000)
            }

            override fun onFinish() {
                onFinish.invoke()
            }

        }
    }

    fun start() {
        if (this::countDownJob.isInitialized) {
            countDownJob.start()
        }
    }

    fun cancel() {
        if (this::countDownJob.isInitialized) {
            countDownJob.cancel()
        }
    }

}